<template>
    <div class="addaddress">
        <div class="addaddress-header">
            <van-nav-bar :title="`${$route.query.name}地址`" left-text="返回" left-arrow  @click-left="$router.back()"/>
        </div>
        <div>
            <van-address-edit :area-list="areaList" show-delete show-set-default show-search-result  :area-columns-placeholder="['请选择', '请选择', '请选择']"  @save="addressInfoHandler" v-if="$route.query.name == '新增'" />
            <van-address-edit :area-list="areaList" :address-info="searchResult" show-delete show-set-default show-search-result  :area-columns-placeholder="['请选择', '请选择', '请选择']"  @save="updateressInfoHandler" v-else/>
        </div>
    </div>
</template>

<script setup lang="ts">
import { areaList } from '@vant/area-data';
import { showToast } from 'vant';
import {updateAddresscheckApi } from '@/api/self'
import {ref} from 'vue'
import { useRoute, useRouter } from 'vue-router';
import { onMounted } from 'vue';
import { LS_Pro } from '@/util/storage';
const someoneInfo = ref<SelfNs.IaddressData>({
    address: '',
    address_detail: '',
    id: 0,
    is_default: 0,
    mobile: '',
    name: '',
})
const searchResult =ref({})
const  router =useRouter()
const route =useRoute()
const someoneInfoParams = ref<SelfNs.IaddressParams>({
    address: '',
    addressId: '',
    checked: false,
    detailadress: '',
    openId: '',
    telNumber: '',
    userName: ''
})
const addressInfoHandler = async(info:any) =>{
    console.log(info);
    someoneInfoParams.value = {
        address: info.province + ' '+ info.city + ' '+ info.county,
        addressId: '',
        checked: info.isDefault,
        detailadress:info.addressDetail,
        openId: '66',
        telNumber: info.tel,
        userName: info.name
    }
    let res =await updateAddresscheckApi(someoneInfoParams.value)
    console.log(res);
    if (res.data == true) {
        showToast({
            message: '添加地址成功哦^_^',
            icon: 'smile-o',
        });
        router.push({name:'address'})
    } else {
        showToast({
            message: '添加地址失败啦~~',
            icon: 'flower-o',
        });
    }
}
const updateressInfoHandler = async(info:any) =>{
    console.log(info);
    someoneInfoParams.value = {
        address: info.province + ' '+ info.city + ' '+ info.county,
        addressId: route.query.id as string,
        checked: info.isDefault,
        detailadress:info.addressDetail,
        openId: '66',
        telNumber: info.tel,
        userName: info.name
    }
    let res =await updateAddresscheckApi(someoneInfoParams.value)
    console.log(res);
    if (res.data == true) {
        showToast({
            message: '编辑地址成功哦^_^',
            icon: 'smile-o',
        });
        router.push({name:'address'})
    } else {
        showToast({
            message: '编辑地址失败啦~~',
            icon: 'flower-o',
        });
    }
}
onMounted(()=>{
    if(route.query.name =='编辑'){
        someoneInfo.value= LS_Pro.get('address').find((item:any) => item.id == route.query.id)
        searchResult.value ={
            name:someoneInfo.value.name,
            tel:someoneInfo.value.mobile,
            province:someoneInfo.value.address.split(' ').join(' ').slice(0,3),
            city:someoneInfo.value.address.split(' ').join().slice(4,7),
            county:someoneInfo.value.address.split(' ').join().slice(8,11),
            addressDetail:someoneInfo.value.address_detail,
            isDefault:someoneInfo.value.is_default
        }
    }
})
</script>

<style scoped></style>